package com.andreamerla.petslife.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.andreamerla.petslife.manager.GoogleFormsManager;
import com.andreamerla.petslife.util.Response;
import com.andreamerla.petslife.util.Utils;
import com.google.android.gms.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: a.merla
 * Date: 24/07/13
 * Time: 9.25
 */
public class VetFormActivity extends ActionBarActivity {
    private EditText etVetName;
    private EditText etVetAddress;
    private EditText etVetOpeningHours;
    private EditText etVetPhone;
    private EditText etVetEmail;
    private EditText etVetWebsite;
    private EditText etVetOtherContacts;
    private Button bSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vet_form_layout);

        etVetName = (EditText) findViewById(R.id.et_vet_name);
        etVetAddress = (EditText) findViewById(R.id.et_vet_address);
        etVetOpeningHours = (EditText) findViewById(R.id.et_vet_opening_hours);
        etVetPhone = (EditText) findViewById(R.id.et_vet_phone);
        etVetEmail = (EditText) findViewById(R.id.et_vet_email);
        etVetWebsite = (EditText) findViewById(R.id.et_vet_website);
        etVetOtherContacts = (EditText) findViewById(R.id.et_vet_other_contacts);
        bSubmit = (Button) findViewById(R.id.b_submit);
        bSubmit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Utils.hideKeyboard(VetFormActivity.this, view);
                if (validateFields())
                    new SubmitFormTask().execute();
                else
                    Toast.makeText(VetFormActivity.this, R.string.vet_form_error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private boolean validateFields() {
        boolean isValid = true;
        if (etVetName.getText().toString().trim().equals("")) {
            etVetName.setError(getString(R.string.vet_name_error));
            isValid = false;
        }
        if (etVetAddress.getText().toString().trim().equals("")) {
            etVetAddress.setError(getString(R.string.vet_address_error));
            isValid = false;
        }
        if (etVetPhone.getText().toString().trim().equals("")) {
            etVetPhone.setError(getString(R.string.vet_phone_error));
            isValid = false;
        }
        return isValid;
    }

    private class SubmitFormTask extends AsyncTask<String, Void, Response> {
//        private static final String FORM_RESPONSE_URL = "https://docs.google.com/forms/d/1_tEfSx6k6b_vMvh4SdZdJUtIwMKnWq0KgtpLQeTaB5o/formResponse"; // TEST ANDROID
        private static final String FORM_RESPONSE_URL = "https://docs.google.com/spreadsheet/formResponse?formkey=dFhOVWlIbGxhVDlrY3hOMEV6ZU9RSUE6MQ&amp;ifq"; // PETS LIFE - VETERINARI ESOTICI

        private Map<String, String> formDataMap = new HashMap<String, String>();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            // TEST ANDROID
//            formDataMap.put("entry.404952164", etVetName.getText().toString());
//            formDataMap.put("entry.1340364391", etVetAddress.getText().toString());
//            formDataMap.put("entry.1603974137", etVetOpeningHours.getText().toString() + "\nSegnalazione inviata dall'applicazione Android");

            // PETS LIFE VETERINARI ESOTICI
            formDataMap.put("entry.0.single", etVetName.getText().toString());
            formDataMap.put("entry.1.single", etVetAddress.getText().toString());
            formDataMap.put("entry.14.single", etVetOpeningHours.getText().toString());
            formDataMap.put("entry.2.single", etVetPhone.getText().toString());
            formDataMap.put("entry.4.single", etVetEmail.getText().toString());
            formDataMap.put("entry.8.single", etVetWebsite.getText().toString());
            formDataMap.put("entry.10.single", etVetOtherContacts.getText().toString() + "\nSegnalazione inviata dall'applicazione Android");
        }

        @Override
        protected Response doInBackground(String... strings) {
            return GoogleFormsManager.submitForm(FORM_RESPONSE_URL, formDataMap);
        }

        @Override
        protected void onPostExecute(Response response) {
            super.onPostExecute(response);

            String message = getString(response.equals(Response.OK) ? R.string.form_submit_ok : R.string.form_submit_error);
            Toast.makeText(VetFormActivity.this, message, Toast.LENGTH_SHORT).show();
        }
    }
}
